<?php defined('SYSPATH') or die('No direct script access.');

class date extends date_Core {

    /**
     * @param <type> $firstDate Date string YYYY-MM-DD
     * @param <type> $firstTime Time string HH:MM[:SS]
     * @param <type> $secondDate Date string YYYY-MM-DD
     * @param <type> $secondTime Time string HH:MM[:SS]
     * @return number of seconds between first and second dateTime. If the second
     * dateTime is before the first one, the returned value is negative.
     */
    public static function diffDateTimes($firstDate, $firstTime, $secondDate, $secondTime) {
        $firstTimestamp = strToTime($firstDate . ' ' . $firstTime);
        $secondTimestamp = strToTime($secondDate . ' ' . $secondTime);
        return $secondTimestamp - $firstTimestamp;
    }
    
    /**
     * @return date from the given timestamp in YYYY-MM-DD format
     */
    public static function timestampToIsoFormat($timestamp) {
    	return date('Y-m-d', $timestamp);
    }

    /**
     * @return number of days between second and first date. If the second
     * date is before the first one, the returned value is negative.
     */
    public static function diffDates($firstDate, $secondDate) {
        $firstTimestamp = strToTime($firstDate);
        $secondTimestamp = strToTime($secondDate);
        return intVal(($secondTimestamp - $firstTimestamp) / (24 * 3600));
    }

    public static function nightCount($firstDate, $lastDate) {
        return self::diffDates($firstDate . ' 1:00', $lastDate . ' 23:00');
    }

    /**
     * Taken from http://snippets.dzone.com/posts/show/1310
     * @param <type> $p_strDate
     */
    public static function personAge($p_strDate) {
        list($Y,$m,$d) = explode("-",$p_strDate);
        $years = date("Y") - $Y;
        if( date("md") < $m.$d ) {
            $years--;
        }
        return $years;
    }

    public static function hourOfTime($time) {
        $hour = trim(substr($time, 0, strpos($time, ':')));
        return $hour;
    }

    public static function isInPast($date, $time='00:00') {
        $timestamp = strToTime($date . ' ' . $time);
        return time() - $timestamp > 0;
    }

    public static function getHour($time) {
        return intval(date('G', strToTime($time)));
    }

    public static function dateIntervalIncludesToday($firstDate, $secondDate) {
         $firstTimestamp = strToTime($firstDate);
         $secondTimestamp = strToTime($secondDate);
         $todayTimestamp = strToTime(sql::$TODAY);
        return $firstTimestamp <= $todayTimestamp && $todayTimestamp <= $secondTimestamp;
    }

    public static function today() {
        return date('Y-m-d');
    }
    
    public static function month($date) {
        $parts = preg_split('/-/', $date);
        return preg_replace('/^0/', '', $parts[1]);
    }
    
    public static function day($date) {
        $parts = preg_split('/-/', $date);
        return preg_replace('/^0/', '', $parts[2]);
    }
    
    public static function year($date) {
        $parts = preg_split('/-/', $date);
        return $parts[0];
    }
    
    /**
     * @return DateTime
     */
    public static function addDays($isoDateString, $numberOfDays) {
    	$timezone = date_default_timezone_get();
    	date_default_timezone_set('UTC');
    	$dayUnixTime = strtotime($isoDateString);
    	date_default_timezone_set($timezone);
    	$dateTime = new DateTime("@" . ($dayUnixTime + 3600 * 24 * $numberOfDays));
    	// $dateTime = $dateTime->setTimestamp($dayUnixTime + 3600 * 24 * $numberOfDays);
    	return $dateTime;
    	// return date('Y-m-d', $dayUnixTime + 3600 * 24 * $numberOfDays);
    }
}